﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.IO;

namespace simWaifu
{
    partial class AboutBox : Form
    {
        public AboutBox()
        {
            InitializeComponent();
            this.Text = String.Format("Info über {0}", AssemblyTitle);
            this.labelProductName.Text = AssemblyProduct;
            this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
            this.labelCopyright.Text = "";// AssemblyCopyright;
            this.labelCompanyName.Text = ""; // AssemblyCompany;
            this.textBoxDescription.Text = AssemblyDescription;
        }

        private void Init()
        {
        }

        #region Assemblyattributaccessoren

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void AboutBox_Load(object sender, EventArgs e)
        {
            DebugMessageHandler.WriteMsgLine("AboutBox_Load()");

            // override version number
            this.labelVersion.Text = "Version: " + Properties.Settings.Default.AppVersion;
            DebugMessageHandler.WriteMsgLine("AppVersion: " + Properties.Settings.Default.AppVersion);

            string appPath = Application.StartupPath;
            string readmeFile = Path.Combine( appPath, "readme.txt");
            
            // create reader & open file
            TextReader tr = null;
            try
            {
                DebugMessageHandler.WriteMsgLine("Trying to read: " + readmeFile);
                tr = new StreamReader(readmeFile);
                string line = tr.ReadLine();
                // clear box if text file was found and has content
                // (if file not found or is empty the description
                // from the assembly info will be used)
                if (line != null)
                    textBoxDescription.Clear();
                while (line != null)
                {
                    textBoxDescription.AppendText(line);
                    textBoxDescription.AppendText(Environment.NewLine);
                    line = tr.ReadLine();
                } 

                // close the stream
                // (now done in finally)
            }
                // gotta catch ´em all
                // why can´t I hold all these exceptions?
            catch (System.ObjectDisposedException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.ObjectDisposedException");
            }
            catch (System.ArgumentNullException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.ArgumentNullException");
            }
            catch (System.ArgumentOutOfRangeException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.ArgumentOutOfRangeException");
            }
            catch (System.ArgumentException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.ArgumentException");
            }
            catch (System.IO.FileNotFoundException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.IO.FileNotFoundException: " + readmeFile);
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.IO.DirectoryNotFoundException: " + readmeFile);
            }
            catch (System.IO.IOException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.IO.IOException");
            }
            catch (System.OutOfMemoryException)
            {
                DebugMessageHandler.WriteError("AboutBox_Load(): ");
                DebugMessageHandler.WriteErrorLine("System.OutOfMemoryException");
            }
            finally
            {
                // make sure stream is closed
                if (tr != null)
                {
                    tr.Close();
                }
            }
        }
    }
}
